/*==============================================================================
FILE NAME: Table_A1.do
CREATED: 25 July 2025
==============================================================================*/


/* Set directory if working independently through code
if c(username)=="" { //insert username
	global rootdir "" // insert root path
	global processed_data "$rootdir/processed_data" 
	global figures "$rootdir/output/figures"  // Define global paths for replication package
} 
*/

// Sample summary statistics
use "$processed_data/Air_Panel.dta", clear


drop if never_air_inv == 1

egen t = group(year month)
xtset RN_id t

forv h = 0/12 {
	gen p_air_inv_`h' = f`h'.p_air_inv - l1.p_air_inv
}

forv h = 2/12 {
	gen p_air_inv_neg`h' = l`h'.p_air_inv - l1.p_air_inv
}

egen RN_year = group(RN year)

unique RN_id
qui reghdfe p_air_inv_0  p_air_incident, absorb(RN_year t) cluster(RN_id)
keep if e(sample)
keep RN_id
duplicates drop
sort RN_id
save "$processed_data/temp.dta", replace


use "$processed_data/Air_Panel.dta", clear
merge m:1 RN_id using "$processed_data/temp.dta"
keep if _merge==3
drop _merge
save "$processed_data/Air_est_sample_NSR_TitleV_investigated.dta", replace
unique RN_id
sum p_air_incident p_air_inv p_air_complaint_inv p_air_nocomplaint_inv p_air_nov p_air_noe p_ee_incident other_air_complaint_region


estpost summarize p_air_incident p_air_inv p_air_complaint_inv p_air_nocomplaint_inv p_air_nov p_air_noe p_ee_incident other_air_complaint_region
esttab using "$tables/Table_A1.tex",  ///
	replace label tex ///
	cells("mean(fmt(4)) sd(fmt(4))") ///
	collabels("Mean" "Std. Dev.")